package com.bnz.security.service;


import com.bnz.security.mapper.UserMapper;
import com.bnz.security.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.*;
import org.springframework.stereotype.Service;

import java.util.Collection;

@Service
public class MyDetailService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        /*if(!"admin".equalsIgnoreCase(username)){
        throw new UsernameNotFoundException("用户名不存在！");
        }*/
       // String password = "$2a$10$8LKEF0/2dTA0.EQhx1yaX.M4uqkIeXb2VucAfvOcplP2LfXr5XP9S";

        Users user=new Users();
        user.setUsername(username);
        Users users =userMapper.selectOne(user);

        Collection<? extends GrantedAuthority> authorities= AuthorityUtils.commaSeparatedStringToAuthorityList("user,admin,ROLE_user,ROLE_admin");;
        return new User(username,users.getPassword(),authorities);
    }
}
